package com.gk.ScheduleTask;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.gk.bean.ZxyBasicinfoGuoyao;
import com.gk.dao.ZxyBasicinfoGuoyaoDao;
import com.gk.http.entity.HttpResult;
import com.gk.service.BaseInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 上传基本信息到国控
 */
@Component
@EnableScheduling
@EnableAsync
@Slf4j
public class UploadGkBasicInfoMultithreadScheduleTask {
    @Resource
    private ZxyBasicinfoGuoyaoDao zxyBasicinfoGuoyaoDao;
    @Resource
    private BaseInfoService baseInfoService;

    //上传商品信息
    @Async
    @Scheduled(fixedDelay = 120000)  //一秒= 1000
    public void UploadPtypeInfo() {
        log.info(DateUtil.now() + ":开始上传商品信息");
        List<ZxyBasicinfoGuoyao> ptypeList = zxyBasicinfoGuoyaoDao.getNeedUpLoadBasicInfoByTypes("P");
        for (ZxyBasicinfoGuoyao zxyBasicinfoGuoyao : ptypeList) {
            HttpResult httpResult = baseInfoService.uploadPtypeInfoByRec(zxyBasicinfoGuoyao.getRec());
            log.info(JSON.toJSONString(httpResult));
            if (httpResult.getCode().equals("0")) {
                zxyBasicinfoGuoyaoDao.delNeedUpLoadBasicInfoByTypes(String.valueOf(zxyBasicinfoGuoyao.getRec()), "P");
            }
        }
        log.info(DateUtil.now() + ":结束上传商品信息");
    }

    //上传往来单位信息
    @Async
    @Scheduled(fixedDelay = 140000)  //一秒= 1000
    public void UploadBtypeInfo() {
        log.info(DateUtil.now() + ":开始上传往来单位信息");

        List<ZxyBasicinfoGuoyao> ptypeList = zxyBasicinfoGuoyaoDao.getNeedUpLoadBasicInfoByTypes("B");
        for (ZxyBasicinfoGuoyao zxyBasicinfoGuoyao : ptypeList) {
            HttpResult httpResult = baseInfoService.uploadBtypeInfoByRec(zxyBasicinfoGuoyao.getRec());
            log.info(JSON.toJSONString(httpResult));
            if (httpResult.getCode().equals("0")) {
                zxyBasicinfoGuoyaoDao.delNeedUpLoadBasicInfoByTypes(String.valueOf(zxyBasicinfoGuoyao.getRec()), "B");
            }
        }
        log.info(DateUtil.now() + ":结束上传往来单位信息");
    }
}
